package com.pda.xinma.ui.outsign;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.pda.xinma.R;
import com.pda.xinma.bean.OutSignBean;
import com.pda.xinma.bean.ScanBean;
import com.pda.xinma.bean.ScanResultBean;
import com.pda.xinma.bean.request.QRcodeRequest;
import com.pda.xinma.contract.OutSignContract;
import com.pda.xinma.dialog.AffirmDialog;
import com.pda.xinma.net.NetUtils;
import com.pda.xinma.presenter.OutSignPresenter;
import com.pda.xinma.view.ScanButView;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.config.StringKey;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;
import com.wangshen.base.util.SharedPreferencesUtil;
import com.wangshen.base.util.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

@Route(path = AppActivityKey.OUTSIGNACTIVITY)
public class OutSignActivity extends BaseMvpActivity<OutSignPresenter> implements OutSignContract.View {
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.tv_storageBin)
    TextView tvStorageBin;
    @BindView(R.id.tv_container)
    TextView tvContainer;
    @BindView(R.id.tv_productModel)
    TextView tvProductModel;
    @BindView(R.id.ll7)
    LinearLayout ll7;
    @BindView(R.id.tv_batch)
    TextView tvBatch;
    @BindView(R.id.ll8)
    LinearLayout ll8;
    @BindView(R.id.tv_indexNo)
    TextView tvIndexNo;
    @BindView(R.id.tv_weight)
    TextView tvWeight;
    @BindView(R.id.tv_actualWeight)
    TextView tvActualWeight;
    @BindView(R.id.tv_apparentDensity)
    TextView tvApparentDensity;
    @BindView(R.id.tv_liquidity)
    TextView tvLiquidity;
    @BindView(R.id.tv_produceDate)
    TextView tvProduceDate;
    @BindView(R.id.tv_shelfLifeDate)
    TextView tvShelfLifeDate;
    @BindView(R.id.sb_scan)
    ScanButView sbScan;
    @BindView(R.id.tv_move_out)
    TextView tvMoveOut;
    @BindView(R.id.tv_deliveryNo)
    TextView tvDeliveryNo;
    @BindView(R.id.tv_receiveBy)
    TextView tvReceiveBy;

    @Override
    public OutSignPresenter getPresenter() {
        return new OutSignPresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_out_sign;
    }

    @Override
    public void initView() {
        String storageBin= SharedPreferencesUtil.getInstance().getString(StringKey.STORAGEBIN,"");
        tvStorageBin.setText(storageBin);
    }

    @Override
    public void initLinsenterner() {
        sbScan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                QRcodeRequest request = new QRcodeRequest();
                request.setQRCode(scanBean.getCode());
                getCode(request);
            }
        });
    }

    public void getCode(QRcodeRequest request) {
        NetUtils.geApi().scanTheQRCodeToVerify(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseAppEntity<ScanResultBean>>() {
                    @Override
                    public void accept(BaseAppEntity<ScanResultBean> stringBaseAppEntity) throws Exception {
                        if (!stringBaseAppEntity.getCode().equals("200")) {
                            ToastUtils.showToast(mContext, stringBaseAppEntity.getMessage());
                            return;
                        }
                        if (stringBaseAppEntity.getData().getType().equals("B")){
                            tvStorageBin.setText(stringBaseAppEntity.getData().getCode());
                            //SharedPreferencesUtil.getInstance().putString(StringKey.STORAGEBIN,stringBaseAppEntity.getData().getCode());
                        }else if (stringBaseAppEntity.getData().getType().equals("A")) {
                            tvContainer.setText(stringBaseAppEntity.getData().getCode());
                            presenter.getInfoByContainer(stringBaseAppEntity.getData().getCode());
                        } else {
                            ToastUtils.showToast(mContext, "请扫正确的条形码");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ToastUtils.showToast(mContext, throwable.getMessage());
                    }
                });
    }

    @Override
    public void getData() {

    }

    @Override
    public void setData(OutSignBean bean) {
        tvBatch.setText(bean.getBatch());
        tvIndexNo.setText(bean.getIndexNo() + "");
        tvProduceDate.setText(bean.getProduceDate());
        tvApparentDensity.setText(bean.getApparentDensity());
        tvLiquidity.setText(bean.getLiquidity());
        tvShelfLifeDate.setText(bean.getShelfLifeDate());
        tvProductModel.setText(bean.getProductModel());
        tvReceiveBy.setText(bean.getReceiveBy());
        tvDeliveryNo.setText(bean.getDeliveryNo());
        tvWeight.setText(bean.getStockNum()+bean.getUnit());
    }

    @Override
    public void onSuccess() {
        tvBatch.setText("");
        tvIndexNo.setText("");
        tvProduceDate.setText("");
        tvApparentDensity.setText("");
        tvLiquidity.setText("");
        tvShelfLifeDate.setText("");
        tvProductModel.setText("");
        tvReceiveBy.setText("");
        tvDeliveryNo.setText("");
        tvWeight.setText("");
        tvContainer.setText("");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.iv_close, R.id.tv_move_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                finishActivity();
                break;
            case R.id.tv_move_out:
                if (TextUtils.isEmpty(tvContainer.getText())){
                    showMessage("请先扫桶码");
                    return;
                }
                if (TextUtils.isEmpty(tvStorageBin.getText())){
                    showMessage("请先扫作业库位");
                    return;
                }
                AffirmDialog dialog=new AffirmDialog(mContext, new AffirmDialog.OnConfrim() {
                    @Override
                    public void onConfrim() {
                        presenter.deliverySignFor(tvStorageBin.getText().toString(),tvContainer.getText().toString());
                    }
                });
                dialog.show("确认要签收吗?");
                break;
        }
    }
}
